<template>
<div>
  <h1>活动</h1>
  <!---搜索表单-->
  <div class="search-div">
    <el-form label-width="70px" size="small">
      <el-row>
        <el-col :span="12">
          <el-form-item label="活动名称">
            <el-input style="width: 300px" v-model="form.title" placeholder="活动名称" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="开始时间-结束时间">
            <el-date-picker
                v-model="createTimes"
                type="datetimerange"
                range-separator="To"
                start-placeholder="开始时间"
                end-placeholder="结束时间"
                format="YYYY-MM-DD HH:mm:ss"
                value-format="YYYY-MM-DD HH:mm:ss"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row style="display:flex">
        <el-button type="primary" size="small" @click="searchSysUser">
          搜索
        </el-button>
        <el-button size="small" @click="resetData">重置</el-button>
        <!--添加按钮-->
        <div class="tools-div">
          <el-button size="small" type="success" @click="ShowActivity">添加活动</el-button>
          <el-button size="small" type="success" @click="overdueUser">禁用</el-button>
          <el-button size="small" type="success" @click="enableUser">列表</el-button>
        </div>
      </el-row>
    </el-form>
  </div>
  <!---数据表格-->
  <el-table :data="list" style="width: 100%">
    <el-table-column prop="title" label="名称" />
    <el-table-column prop="avatar" label="图片">
      <template #default="scope">
        <el-image :src="scope.row.avatar" style="width: 100px;height: 50px" :preview-src-list="[scope.row.avatar]" />
      </template>
    </el-table-column>
    <el-table-column prop="integral" label="积分" />
    <el-table-column prop="createtime" label="开始时间" />
    <el-table-column label="操作" align="center" width="280" #default="scope">
      <el-button type="danger" size="small" @click="updateById(scope.row)">
        {{ scope.row.state == 0 ? '禁用' : '启用' }}
      </el-button>
      <el-button type="danger" v-show="scope.row.state===1" size="small" @click="deleteById(scope.row)">
        删除
      </el-button>
    </el-table-column>
  </el-table>
  <!--分页条-->
  <el-pagination
      v-model:current-page="pageParamsList.page"
      v-model:page-size="pageParamsList.limit"
      :page-sizes="[2, 20, 50, 100]"
      @size-change="fetchSysUser"
      @current-change="fetchSysUser"
      layout="total, sizes, prev, pager, next"
      :total="total"
  />
  <el-dialog v-model="dialogVisible" title="添加卡券类型" width="30%">
    <el-form label-width="120px" :model="addActivityForm">
      <el-form-item label="活动名称">
        <el-input v-model="addActivityForm.title" placeholder="请输入活动名称"/>
      </el-form-item>
      <el-form-item label="活动链接">
        <el-input v-model="addActivityForm.avatar" placeholder="请输入活动链接"/>
      </el-form-item>
      <el-form-item label="活动积分">
        <el-input v-model="addActivityForm.integral" placeholder="请输入活动积分"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="saveActivity">提交</el-button>
        <el-button @click="dialogVisible = false">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</div>
</template>

<script setup>

//开始和结束时间数据模型
import {onMounted, ref} from "vue";
import {ElMessage, ElMessageBox} from "element-plus";
import {getCouponsUpdateState, getDeleteCoupons} from "@/api/changebattery/coupons";
import {getActivityList, getAddActivity, getDeleteActivity, getUpdateActivityState} from "@/api/changebattery/activity";

const createTimes = ref([])
const list = ref([])

const dialogVisible =ref(false)

const addActivity={
  title:'',
  avatar:'',
  integral:'',
}
const addActivityForm =ref(addActivity)

const saveActivity = async  () =>{
  const {code} = await getAddActivity(addActivityForm.value)
  if (code === 200){
    ElMessage.success('添加成功')
    dialogVisible.value=false
    await fetchSysUser()
  }
}

const ShowActivity = () =>{
  dialogVisible.value=true
}

const queryDto = {
  createTimeBegin: '',
  createTimeEnd: '',
  state:0,
  title:'',
}

const form = ref(queryDto)
//重置
const resetData = async () => {
  form.value.title = ''
  form.value.createTimeBegin = ''
  form.value.createTimeEnd = ''
  createTimes.value=[]
  await fetchSysUser()
}
// 删除数据
const deleteById = row => {
  ElMessageBox.confirm('此操作将永久删除该记录, 是否继续?', 'Warning', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(async () => {
    const { code } = await getDeleteActivity(row.id)
    if (code === 200) {
      ElMessage.success('删除成功')
      pageParamsUsers.value.page = 1
      await fetchSysUser()
    }
  })
}
//上架下架
const updateById = async (row) => {
  const { code } = await getUpdateActivityState(row.id)
  ElMessage.success('操作成功')
  await fetchSysUser(pageParamsUsers.value.page,
      pageParamsUsers.value.limit,
      form.value)
}

//否用用户
const overdueUser = () =>{
  form.value.state=1
  fetchSysUser()
}

//启用用户
const enableUser = () =>{
  form.value.state=0
  fetchSysUser()
}
//搜索方法
const searchSysUser = () => {
  fetchSysUser(
      pageParamsUsers.value.page,
      pageParamsUsers.value.limit,
      form.value
  )
}

//列表
const fetchSysUser = async () => {
  //获取开始和结束时间
  if (createTimes.value.length == 2) {
    form.value.createTimeBegin = createTimes.value[0]
    form.value.createTimeEnd = createTimes.value[1]
  }
  const { data } = await getActivityList(
      pageParamsUsers.value.page,
      pageParamsUsers.value.limit,
      form.value
  )
  list.value = data.records
  total.value = data.total
}
onMounted(async () => {
  await fetchSysUser()
})
//分页数据
const pageParamsList = {
  page: 1,
  limit: 10,
}
const total = ref(0)
const pageParamsUsers = ref(pageParamsList)
</script>

<style scoped>

</style>
